{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "a34ded0b-5447-4c67-9b8d-4c6c4e6a1df6",
   "metadata": {},
   "outputs": [],
   "source": [
    "from transformers import pipeline\n",
    "import torch\n",
    "device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "a6a1156e-79ab-4fc1-8538-3a9869e8d0a3",
   "metadata": {},
   "outputs": [],
   "source": [
    "pipeline_instance = pipeline(model=\"facebook/bart-large-mnli\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "26e91bcd-38c4-4d6a-877b-ea81be9e36d4",
   "metadata": {},
   "outputs": [],
   "source": [
    "result = pipeline_instance(\"I am so hooked to video games as I cannot get any work done!\",\n",
    " candidate_labels=[\"technology\", \"gaming\", \"hobby\", \"art\", \"computer\"], device=device)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "0c9e3b90-8fcc-4d65-9ffb-bb41dd4c205a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "I am so hooked to video games as I cannot get any work done!\n",
      "gaming:  0.85\n",
      "hobby:  0.08\n",
      "technology:  0.07\n",
      "computer:  0.00\n",
      "art:  0.00\n"
     ]
    }
   ],
   "source": [
    "print(result['sequence'])\n",
    "for i, label in enumerate(result['labels']):\n",
    "   print(f\"{label}:  {result['scores'][i]:.2f}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "72b655f2-34e3-4f82-a8d9-11fd157fd92b",
   "metadata": {},
   "outputs": [],
   "source": [
    "result = pipeline_instance(\"A early morning exercise regimen can drive many diseases away!\",\n",
    "             candidate_labels=[\"health\", \"medical\", \"weather\", \"geography\", \"politics\"], )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "0198f34b-7ec2-4929-a691-4dc0dd17c66c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "A early morning exercise regimen can drive many diseases away!\n",
      "health:  0.91\n",
      "medical:  0.07\n",
      "weather:  0.01\n",
      "geography:  0.01\n",
      "politics:  0.00\n",
      "The most probable class for the sentence is ** health ** with a probability of 0.91\n"
     ]
    }
   ],
   "source": [
    "print(result['sequence'])\n",
    "for i, label in enumerate(result['labels']):\n",
    "   print(f\"{label}:  {result['scores'][i]:.2f}\")\n",
    "print(f\"The most probable class for the sentence is ** {result['labels'][0]} ** with a probability of {result['scores'][0]:.2f}\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
